﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AD.Contract.Models;
using AD.Contract;
using AD.DAL;
using EntityFramework.Extensions;
using AD.IBLL;
using HZT.Framework.Contract;
namespace AD.BLL
{
    public class CustomerService
    {
        public Customer GetModel(string id)
        {
            using (var dbcontext = new ADDbContext())
            {
                return dbcontext.Find<Customer>(id);
            }
        }
        public IEnumerable<Customer> GetList(CommonRequest request = null)
        {
            request = request ?? new CommonRequest();
            using (var dbContext = new ADDbContext())
            {
                IQueryable<Customer> models = dbContext.Customers;

                if (!string.IsNullOrEmpty(request.Name))
                {
                    models = models.Where(u => u.Name.Contains(request.Name));
                }

                return models.OrderByDescending(u => u.Id).ToPagedList(request.PageIndex, request.PageSize);
            }
        }
        public void Save(Customer model)
        {
            using (var dbcontext = new ADDbContext())
            {
                if (!string.IsNullOrEmpty(model.Id))
                {
                    dbcontext.Update<Customer>(model);
                }
                else
                {
                    model.Id = Guid.NewGuid().ToString();

                    dbcontext.Insert<Customer>(model);
                }
            }

        }
        public void Delete(List<string> ids)
        {
            using (var dbContext = new ADDbContext())
            {
                dbContext.Customers.Where(u => ids.Contains(u.Id)).Delete();
            }
 
        }
    }
}
